<template>
  <div>
    <avue-crud
      :option="optionDistributionStatement"
      :table-loading="loading"
      :data="data"
      :page="page"
      @row-del="rowDel"
      v-model="form"
      :permission="permissionList"
      :before-open="beforeOpen"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
      <template #menuRight>
        <el-button type="primary" @click="isSetting(row)" size="small"
          >账单生成</el-button
        >
      </template>
      <template #menu="{ row, index, size }">
        <el-button type="text" icon="el-icon-view" @click="openView(row)"
          >查看</el-button
        >
        <el-button
          type="text"
          icon="el-icon-document-checked"
          @click="confirmBill(row)"
          >账单确认</el-button
        >
        <el-button type="text" icon="el-icon-document-checked" @click="pay(row)"
          >支付</el-button
        >
      </template>
      <template #menuLeft>
        <el-form :inline="true" :model="query" class="demo-form-inline">
          <el-form-item>
            <el-date-picker
              size="small"
              v-model="searchDate"
              type="daterange"
              class="m-r-10"
              value-format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="conditionalQuery"
            />
          </el-form-item>
          <el-form-item label="">
            <el-select
              @change="conditionalQuery"
              v-model="query.payStatus"
              placeholder="全部"
              size="small"
            >
              <el-option label="全部" value=""></el-option>
              <el-option label="未付款" value="1"></el-option>
              <el-option label="已付款" value="2"></el-option>
              <el-option label="付款失败" value="3"></el-option>
              <el-option label="付款中" value="4"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <div class="w-50 m-t-20">
          <el-descriptions column="2" :colon="false">
            <el-descriptions-item
              label="账单总金额(元)"
              content-class-name="c-blue f-s-20 lh-1"
              >{{ totalBillMoney }}</el-descriptions-item
            >
          </el-descriptions>
        </div>
      </template>
    </avue-crud>
    <el-dialog
      title="支付"
      append-to-body
      :visible.sync="payDialog"
      width="400px"
    >
      <el-form :model="form" label-width="80px">
        <el-form-item label="支付方式">
          <el-radio-group v-model="form.payType">
            <el-radio :label="0">微信支付</el-radio>
            <el-radio :label="1">线下支付</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <div class="dividing-line m-t-20 m-b-20"></div>
      <div class="flex-end">
        <el-button
          type="primary"
          size="small"
          :loading="adjustLoading"
          @click="doAdjust"
          >确定</el-button
        >
        <el-button type="primary" size="small" plain @click="payDialog = false"
          >取消</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { isListEmpty } from "@/util/common-util";
import {
  getList,
  summary,
  updateSettled,
} from "@/api/financial/distribution-statement";
import { optionDistributionStatement } from "@/const/financial/distribution-statement";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      optionDistributionStatement,
      showSettingFlag: false,
      data: [],
      searchDate: null,
      optionIsSettled: [
        {
          label: "全部",
          value: undefined,
        },
        {
          label: "未结算",
          value: 0,
        },
        {
          label: "已结算",
          value: 1,
        },
      ],
      payDialog: false,
      totalBillMoney: 0,
      adjustLoading: false,
    };
  },
  computed: {
    ...mapGetters(["permission"]),
  },
  methods: {
    confirmBill(row) {
      this.$confirm("账单确认?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "设置成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    doAdjust() {
      this.adjustLoading = false;
      updateSettled(this.form)
        .then((response) => {
          this.$message({
            type: "success",
            message: "设置成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    pay(row) {
      this.form = {
        id: row.id,
      };
      this.payDialog = true;
    },
    openView(row) {
      this.$emit("openDistributionStatementDetail", row);
    },
    //账单结算
    isSetting(row) {
      this.showSettingFlag = true;
    },
    //搜索查询
    conditionalQuery() {
      if (isListEmpty(this.searchDate)) {
        this.query.startTime = "";
        this.query.endTime = "";
      } else {
        this.query.startTime = this.searchDate[0];
        this.query.endTime = this.searchDate[1];
      }
      this.onLoad(this.page, this.query);
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
      summary(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        this.totalBillMoney = res.data.data.totalBillMoney;
      });
    },
  },
};
</script>

<style lang="scss">
.lh-1 {
  line-height: 20px !important;
  padding-bottom: 50px;
}
</style>
